package com.examinantsdk.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.examinantsdk.base.view.ClassicsFooter;
import com.examinantsdk.base.view.CustomRefreshHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;


public class MyApplication extends Application {
    public static Context context;

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> new CustomRefreshHeader(context));
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> new ClassicsFooter(context));
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

    }

    public static String getToken() {
        return SPUtils.getInstance().getString("token");
    }
    public static void setToken(String token) {
        SPUtils.getInstance().put("token", token);
    }

    public static void invalidLogin() {
        MyApplication.setToken("");
    }

    public static boolean checkLogin(Activity activity) {
        if (activity == null) return false;
        if (TextUtils.isEmpty(getToken())) {
//            UMLogin.get().umLogin(activity, "");
            return true;
        }
        return false;
    }

    /**
     * 获取版本号名称
     */
    public static String getVersionName() {
        String versionName = "";
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    private static long exitTime = 0;
    public static boolean fastClick(int time) {
        if (System.currentTimeMillis() - exitTime > time) {
            exitTime = System.currentTimeMillis();
        } else {
            return true;
        }
        return false;
    }

}
